package com.yupi.springbootinit.bizmq;

import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.yupi.springbootinit.constant.BiMqConstant;

/**
 * ClassName: MqInitMain
 * Package: com.yupi.springbootinit.bizmq
 * Description:消息队列初始化。用于测试程序中的交换机和队列（只用在程序执行前执行一次）
 *
 * @Author:
 * @Create: 2024/4/4 - 21:07
 * @Version: v1.0
 */
public class BiMqInitMain {

    public static void main(String[] args) {

        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            Connection connection = factory.newConnection();
            Channel channel = connection.createChannel();

            channel.exchangeDeclare(BiMqConstant.BI_EXCHANGE_NAME, BuiltinExchangeType.DIRECT);

            //创建队列
            channel.queueDeclare(BiMqConstant.BI_QUEUE_NAME, true, false, false, null);
            channel.queueBind(BiMqConstant.BI_QUEUE_NAME, BiMqConstant.BI_EXCHANGE_NAME, BiMqConstant.BI_ROUTING_KEY);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
